use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};

use axum::extract::FromRef;
use carpet::server::{Server, define_handler_straw};
use log::info;

pub struct DemoServer {}

#[derive(Debug, FromRef, Clone)]
pub struct AppState {
    pub db: String,
}

define_handler_straw!(AppState);

impl Server for DemoServer {
    type State = AppState;

    type HandlerStraw = MethodHandlerStrawImpl;

    fn startup_message(&self, _addr: &SocketAddr) {
        info!("启动完成");
    }

    fn addr(&self) -> Result<SocketAddr, anyhow::Error> {
        Ok(std::net::SocketAddr::V4(SocketAddrV4::new(
            Ipv4Addr::from([0, 0, 0, 0]),
            8030,
        )))
    }

    fn state(&self) -> Self::State {
        AppState {
            db: String::default(),
        }
    }
}
